package net.piencak.phonetools.gpscc;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

public class ConvertForm extends Form implements CommandListener, ItemCommandListener {
	
	private GPSCC app;
	
	private TextField decDegDegreesTextField = new TextField("DDD:", "", 3, TextField.NUMERIC); 
	private TextField decDegDecimalsTextField = new TextField("DDDDD:", "", 5, TextField.NUMERIC); 

	private TextField degMinDegreesTextField = new TextField("DDD:", "", 3, TextField.NUMERIC); 
	private TextField degMinMinutesTextField = new TextField("MM:", "", 2, TextField.NUMERIC); 
	private TextField degMinDecimalsTextField = new TextField(".MMMM:", "", 4, TextField.NUMERIC); 

	private TextField dmsDegreesTextField = new TextField("DDD:", "", 3, TextField.NUMERIC); 
	private TextField dmsMinutesTextField = new TextField("MM:", "", 2, TextField.NUMERIC); 
	private TextField dmsSecondsTextField = new TextField("SS:", "", 2, TextField.NUMERIC); 
	private TextField dmsDecimalsTextField = new TextField(".SSSS:", "", 4, TextField.NUMERIC); 
	
	private Command convertFromDecDeg = new Command("DecDeg >>",Command.ITEM, 1);
	private Command convertFromDegMin = new Command("DegMin >>",Command.ITEM, 1);
	private Command convertFromDMS = new Command("DMS >>",Command.ITEM, 1);
	
	private StringItem decDegLabel = new StringItem("", "DecDeg:");
	private StringItem degMinLabel = new StringItem("", "DegMin:");
	private StringItem dmsLabel = new StringItem("", "DMS:");

	public ConvertForm(GPSCC app) {
		super("GPS Converter");
		
		this.app = app;
		
		setCommandListener(this);
		
		decDegLabel.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
		decDegLabel.setDefaultCommand(convertFromDecDeg);
		decDegLabel.setItemCommandListener(this);
		append(decDegLabel);	
		decDegDegreesTextField.setLayout(Item.LAYOUT_2);
		decDegDegreesTextField.setDefaultCommand(convertFromDecDeg);
		decDegDegreesTextField.setItemCommandListener(this);
		append(decDegDegreesTextField);
		decDegDecimalsTextField.setLayout(Item.LAYOUT_2);
		decDegDecimalsTextField.setDefaultCommand(convertFromDecDeg);
		decDegDecimalsTextField.setItemCommandListener(this);
		append(decDegDecimalsTextField);
		StringItem stringItem = new StringItem("", ">", Item.BUTTON);
		stringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
		stringItem.setDefaultCommand(convertFromDecDeg);
		stringItem.setItemCommandListener(this);
		append(stringItem);	

		degMinLabel.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
		degMinLabel.setDefaultCommand(convertFromDegMin);
		degMinLabel.setItemCommandListener(this);
		append(degMinLabel);	
		degMinDegreesTextField.setLayout(Item.LAYOUT_2);
		degMinDegreesTextField.setDefaultCommand(convertFromDegMin);
		degMinDegreesTextField.setItemCommandListener(this);
		append(degMinDegreesTextField);
		degMinMinutesTextField.setLayout(Item.LAYOUT_2);
		degMinMinutesTextField.setDefaultCommand(convertFromDegMin);
		degMinMinutesTextField.setItemCommandListener(this);
		append(degMinMinutesTextField);
		degMinDecimalsTextField.setLayout(Item.LAYOUT_2);
		degMinDecimalsTextField.setDefaultCommand(convertFromDegMin);
		degMinDecimalsTextField.setItemCommandListener(this);
		append(degMinDecimalsTextField);
		stringItem = new StringItem("", ">", Item.BUTTON);
		stringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
		stringItem.setDefaultCommand(convertFromDegMin);
		stringItem.setItemCommandListener(this);
		append(stringItem);	

		dmsLabel = new StringItem("", "DMS:");
		dmsLabel.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
		dmsLabel.setDefaultCommand(convertFromDMS);
		dmsLabel.setItemCommandListener(this);
		append(dmsLabel);
		dmsDegreesTextField.setLayout(Item.LAYOUT_2);
		dmsDegreesTextField.setDefaultCommand(convertFromDMS);
		dmsDegreesTextField.setItemCommandListener(this);
		append(dmsDegreesTextField);
		dmsMinutesTextField.setLayout(Item.LAYOUT_2);
		dmsMinutesTextField.setDefaultCommand(convertFromDMS);
		dmsMinutesTextField.setItemCommandListener(this);
		append(dmsMinutesTextField);
		dmsSecondsTextField.setLayout(Item.LAYOUT_2);
		dmsSecondsTextField.setDefaultCommand(convertFromDMS);
		dmsSecondsTextField.setItemCommandListener(this);
		append(dmsSecondsTextField);
		dmsDecimalsTextField.setLayout(Item.LAYOUT_2);
		dmsDecimalsTextField.setDefaultCommand(convertFromDMS);
		dmsDecimalsTextField.setItemCommandListener(this);
		append(dmsDecimalsTextField);
		stringItem = new StringItem("", ">", Item.BUTTON);
		stringItem.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
		stringItem.setDefaultCommand(convertFromDMS);
		stringItem.setItemCommandListener(this);
		append(stringItem);	
		
		addCommand(new Command("EXIT", Command.EXIT, 0));
		addCommand(new Command("?", Command.SCREEN, 2));
		
		setCommandListener(this);
	}

	public void commandAction(Command com, Displayable dis) {
		String label = com.getLabel();

		if ("EXIT".equalsIgnoreCase(label)) {
			app.notifyDestroyed();
		} else if ("?".equals(label)) {
			app.showAbout();
		}
	}

	public void commandAction(Command c, Item item) {
		if (c == convertFromDecDeg) {
			convertFromDecDeg();
		} else if (c == convertFromDegMin) {
			convertFromDegMin();
		} else if (c == convertFromDMS) {
			convertFromDMS();
		} else {
			System.out.println("???");
		}
	}
	
	private void convertFromDecDeg() {
		double decDeg = Float.parseFloat(decDegDegreesTextField.getString() + "." + decDegDecimalsTextField.getString());
		double decDegDecimals = Float.parseFloat("0." + decDegDecimalsTextField.getString());
		double minutes = decDegDecimals * 60;
		double minutesDecimals = minutes - Math.floor(minutes);
		double seconds = minutesDecimals * 60;
		double secondsDecimals = seconds - Math.floor(seconds);
		
		degMinDegreesTextField.setString(decDegDegreesTextField.getString());
		degMinMinutesTextField.setString(String.valueOf((int)Math.floor(minutes)));
		//TODO: round!
		degMinDecimalsTextField.setString(String.valueOf((int)Math.floor(minutesDecimals * 10000)));
		
		dmsDegreesTextField.setString(decDegDegreesTextField.getString());
		dmsMinutesTextField.setString(String.valueOf((int)Math.floor(minutes)));
		dmsSecondsTextField.setString(String.valueOf((int)Math.floor(seconds)));
		//TODO: round!
		dmsDecimalsTextField.setString(String.valueOf((int)Math.floor(secondsDecimals * 10000)));
	}

	private void convertFromDegMin() {
		double minutes = Float.parseFloat(degMinMinutesTextField.getString());
		double minutesDecimals = Float.parseFloat("0." + degMinDecimalsTextField.getString());
		minutes += minutesDecimals;
		
		decDegDegreesTextField.setString(degMinDegreesTextField.getString());
		decDegDecimalsTextField.setString(String.valueOf(
				(int) Math.floor(100000 * (minutes / 60))
				));
		
		double seconds = minutesDecimals * 60;
		double secondsDecimals = seconds - Math.floor(seconds);
		
		dmsDegreesTextField.setString(degMinDegreesTextField.getString());
		dmsMinutesTextField.setString(String.valueOf((int)Math.floor(minutes)));
		dmsSecondsTextField.setString(String.valueOf((int)Math.floor(seconds)));
		//TODO: round!
		dmsDecimalsTextField.setString(String.valueOf((int)Math.floor(secondsDecimals * 10000)));
	}

	private void convertFromDMS() {
		double seconds = Float.parseFloat(dmsSecondsTextField.getString());
		double secondsDecimals = Float.parseFloat("0." + dmsDecimalsTextField.getString());
		seconds += secondsDecimals;
		
		double minutes = Float.parseFloat(dmsMinutesTextField.getString());
		double minutesDecimals = seconds / 60;
		minutes += minutesDecimals;
		
		decDegDegreesTextField.setString(dmsDegreesTextField.getString());
		decDegDecimalsTextField.setString(String.valueOf(
				(int) Math.floor(100000 * (minutes / 60))
				));
		
		degMinDegreesTextField.setString(dmsDegreesTextField.getString());
		degMinMinutesTextField.setString(String.valueOf((int)Math.floor(minutes)));
		//TODO: round!
		degMinDecimalsTextField.setString(String.valueOf((int)Math.floor(minutesDecimals * 10000)));
	}
	
}
